package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Consumer;
import com.fc.campusapplicationsystem.model.ReList;
import com.fc.campusapplicationsystem.service.ReListService;
import com.fc.campusapplicationsystem.service.ServicemanService;
import com.fc.campusapplicationsystem.util.FileUpload;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.fc.campusapplicationsystem.util.WebUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 贺哲
 * @2020-02-29 15:29
 */
@RestController
@RequestMapping("/reListController")
public class ReListController {

    @Autowired
    ReListService reListService;

    @Autowired
    ServicemanService servicemanService;
    private Integer id;

    @PostMapping(value = "/upload")
    @ResponseBody
    public FileUpload<String> uploads(@RequestParam("file") MultipartFile file) {
        FileUpload<String> res = new FileUpload<>();
        try {
            res.setData(reListService.fileUpload(file));
            res.setCode(0);
            res.setMsg("上传成功");
        } catch (Exception e) {
            res.setCode(-1);
            res.setMsg("上传失败");
        }
        return res;
    }

    /**
     * 用户新增报修单
     *
     * @param reList
     */
    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody ReList reList) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        Consumer consumer = (Consumer) WebUtil.getSession().getAttribute("consumer");
        reList.setUserId(consumer.getId())
                .setStartTime(new Date())
                .setState(0);
        try {
            Integer result = reListService.add(reList);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    /**
     * 修改报修单
     *
     * @param reList
     */
    @PostMapping("/update")
    public ResponseOBJ<Integer> update(@RequestBody ReList reList) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        Consumer consumer = (Consumer) WebUtil.getSession().getAttribute("consumer");
        reList.setUserId(consumer.getId())
                .setStartTime(new Date())
                .setState(0);
        try {
            Integer result = reListService.updateRelist(reList);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    /**
     * 删除报修单
     */
    @DeleteMapping("/delete")
    public ResponseOBJ<Integer> delete(@RequestParam("repairId") Integer id) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = reListService.delete(id);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    @GetMapping("/list")
    public ResponseOBJ<List<ReList>> list(@RequestParam("page") Integer page,
                                          @RequestParam("limit") Integer limit,
                                          @RequestParam(value = "key", required = false) String key) {
        ResponseOBJ<List<ReList>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<ReList> reLists = reListService.list(key);
            Collections.reverse(reLists);
            PageInfo<ReList> pageInfo = new PageInfo<>(reLists);
            responseOBJ.setData(reLists);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }


    /**
     * 查询我的报修
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/myRepair")
    public ResponseOBJ<List<ReList>> myRepair(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        ResponseOBJ<List<ReList>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<ReList> reLists = reListService.myRepair();
            Collections.reverse(reLists);
            PageInfo<ReList> pageInfo = new PageInfo<>(reLists);
            responseOBJ.setData(reLists);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }


    /**
     * 查询我的指派维修任务
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/myAssign")
    public ResponseOBJ<List<ReList>> myAssign(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        ResponseOBJ<List<ReList>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<ReList> reLists = reListService.myAssign();
            Collections.reverse(reLists);
            PageInfo<ReList> pageInfo = new PageInfo<>(reLists);
            responseOBJ.setData(reLists);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }


    @PostMapping("/assign")
    public ResponseOBJ<Integer> assign(@RequestBody ReList reList) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = reListService.assign(reList);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

}
